package online.misscl.lsy.controller.back;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import online.misscl.lsy.controller.BaseController;
import online.misscl.lsy.entity.HospitalInformation;
import online.misscl.lsy.entity.dto.AddHospitalInformationDTO;
import online.misscl.lsy.entity.dto.GetHospitalInformationDTO;
import online.misscl.lsy.entity.dto.UpdateHospitalInformationDTO;
import online.misscl.lsy.entity.dto.vo.HospitalInformationVo;
import online.misscl.lsy.entity.dto.vo.TableDataInfoVo;
import online.misscl.lsy.service.HospitalInformationService;
import online.misscl.lsy.util.AjaxResult;
import online.misscl.lsy.util.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院信息表 前端控制器
 * </p>
 *
 * @author misscl
 * @since 2023/03/11 11:14
 */
@RestController
@RequestMapping("/back/hospitalInformation")
@Api(tags = "后台医院模块")
public class BackHospitalInformationController extends BaseController {
    @Autowired
    private HospitalInformationService hospitalInformationService;

    @ApiOperation(value = "获取医院信息列表")
    @GetMapping("/getHospitalInfo")
    public TableDataInfoVo<List<HospitalInformationVo>> getHospitalInfo(GetHospitalInformationDTO getHospitalInformationDTO) {
        List<HospitalInformationVo> list = hospitalInformationService.getHospitalInfo(getHospitalInformationDTO);
        Page page = PageUtil.getPages(getHospitalInformationDTO.getPageNum(),getHospitalInformationDTO.getPageSize(),list);
        return getDataTable(page.getRecords(),page.getTotal());
    }

    @ApiOperation(value = "根据id获取医院信息")
    @GetMapping("/getHospital/{hospitalId}")
    public AjaxResult getHospital(@PathVariable int hospitalId) {
        HospitalInformation h = hospitalInformationService.getBaseMapper().selectById(hospitalId);
        HospitalInformationVo hvo = new HospitalInformationVo();
        BeanUtils.copyProperties(h,hvo);
        return AjaxResult.success(hvo);
    }

    @ApiOperation(value = "添加医院")
    @PostMapping("/addHospital")
    public AjaxResult addHospital(@RequestBody AddHospitalInformationDTO addHospitalInformationDTO) {
        HospitalInformation hospitalInformation = new HospitalInformation();
        BeanUtils.copyProperties(addHospitalInformationDTO,hospitalInformation);
        //判断重复
        LambdaQueryWrapper<HospitalInformation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<HospitalInformation> eq = lambdaQueryWrapper.eq(HospitalInformation::getHospitalName, addHospitalInformationDTO.getHospitalName());
        HospitalInformation one = hospitalInformationService.getOne(eq);
        if (one != null){
            return AjaxResult.error("医院已存在！请重新输入");
        }else{
            return toAjax(hospitalInformationService.save(hospitalInformation));
        }
    }

    @ApiOperation(value = "修改医院")
    @PutMapping("/getHospitalInfo")
    public AjaxResult updateHospitalInfo(@RequestBody UpdateHospitalInformationDTO updateHospitalInformationDTO) {
        HospitalInformation hospitalInformation = new HospitalInformation();
        BeanUtils.copyProperties(updateHospitalInformationDTO,hospitalInformation);
        return toAjax(hospitalInformationService.updateById(hospitalInformation));
    }

    @ApiOperation(value = "删除医院")
    @PutMapping("/delHospital/{hospitalId}")
    public AjaxResult updateHospitalInfo(@PathVariable Integer hospitalId) {
        return toAjax(hospitalInformationService.removeById(hospitalId));
    }

}
